package com.jiangjiesheng.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 自定义Realm
 */
public class CustomRealm extends AuthorizingRealm {

    Map<String, String> userMap = new HashMap<String, String>(16);

    public static void main(String[] args) {
        System.out.println(new Md5Hash("123456", "salt").toString());//e10adc3949ba59abbe56e057f20f883e
    }

    {   // f51703256a38e6bab3d9410a070c32ea 加盐前
        // e10adc3949ba59abbe56e057f20f883e 加盐后
        userMap.put("js", "f51703256a38e6bab3d9410a070c32ea");// 123456 使用Shiro加密后的密码，实际应该是数据库中的
        //super.setName("customRealmName"); // 似乎不设置也没有问题
    }

    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) principalCollection.getPrimaryPrincipal();
        Set<String> roles = getRolesByUserName(userName);
        Set<String> permissions = getPermissionsByUserName(userName);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);
        return simpleAuthorizationInfo;
    }

    // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 1. 从主体传过来的认证信息中，获取用户名
        String userName = (String) authenticationToken.getPrincipal();
        // 2. 通过用户名到数据库中获取凭证
        String password = getPasswordByUserName(userName);
        // 不要直接通过用户名和密码去查询，这样不好判断是密码错误还是用户名不存在
        // 通过select password的方式，可以判断这两种情况
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo("js", password, "customRealmName");

        //有加盐时
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("salt"));

        return authenticationInfo;
    }

    private String getPasswordByUserName(String userName) {
        //模拟读取数据库
        return userMap.get(userName);
    }

    private Set<String> getRolesByUserName(String userName) {
        //从数据库或者缓存中获取角色数据
        Set<String> sets = new HashSet<String>();
        sets.add("admin");
        sets.add("user");
        return sets;
    }

    private Set<String> getPermissionsByUserName(String userName) {
        //从数据库或者缓存中获取角色数据
        Set<String> sets = new HashSet<String>();
        sets.add("user:delete");
        sets.add("user:add");
        return sets;
    }
}
